package cn.fl.insurance.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.insurance.vo.InsPlcStlDtlVO;
import cn.lb.framework.poi.excel.ExcelImportUtil;
import cn.lb.framework.poi.excel.entity.ImportParams;
/**
 * Function: 保单结算记录数据导入
 *
 */
public class ImportInsuranceList {

	private static final Logger logger = LoggerFactory.getLogger(ImportInsuranceList.class);

	/**
	 * 导入保单结算明细记录数据
	 * 
	 * @param filePath 源文件路径
	 * @return
	 */
	public static List<InsPlcStlDtlVO> importInsPlcStlDtlXlsx(String filePath) {
		logger.info("导入结算记录=" + filePath);
		ImportParams params = new ImportParams();
		params.setStartRows(0);
		params.setTitleRows(2);// 标题占用行数
		params.setHeadRows(1);// 表头占用行数
		params.setNeedSave(false);// 是否保存文件
		List<InsPlcStlDtlVO> insPlcStlDtlList=null;//保单结算明细
		if (!filePath.contains("http")) {
			File file = new File(PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH)+filePath);
            if(file.exists()){
            	insPlcStlDtlList=ExcelImportUtil.importExcel(file, InsPlcStlDtlVO.class, params);
            }
		}else{
			InputStream ins=null;
			URLConnection connection=null;
			try {
				URL url=new URL(filePath);
				connection=url.openConnection();//打开连接 获得URLConnection类的对象
				connection.setConnectTimeout(10000);
				connection.setReadTimeout(60000);
				ins=connection.getInputStream();
				insPlcStlDtlList=ExcelImportUtil.importExcel(ins, InsPlcStlDtlVO.class, params);
			}catch(IOException e){
				BaseLogUtils.error(logger, "importXlsx", "导入保单结算记录数据上传文件出错,"+e.getMessage());
			BaseLogUtils.error(logger, "-", "异常", e);
			}catch (Exception e) {
				BaseLogUtils.error(logger, "importXlsx", "导入保单结算记录数据上传文件出错,"+e.getMessage());
			BaseLogUtils.error(logger, "-", "异常", e);
			}finally{
				if(ins!=null){
					try {
						ins.close();
					} catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
					}
				}
			}
		}
		if(CollectionUtils.isNotEmpty(insPlcStlDtlList)){
			BaseLogUtils.info(logger, "importXlsx", "导入保单结算记录="+filePath+",结算条数="+insPlcStlDtlList.size());
		}else{
			BaseLogUtils.info(logger, "importXlsx", "导入保单结算记录="+filePath+",结算条数=0");
		}
		return insPlcStlDtlList;

	}
}
